package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.News;
import com.mars.module.admin.entity.UserCollection;
import com.mars.module.admin.entity.UserLike;
import com.mars.module.admin.mapper.NewsMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.UserLikeRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.UserLikeMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.UserLike;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IUserLikeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户点赞业务层处理
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserLikeServiceImpl implements IUserLikeService {

    private final UserLikeMapper baseMapper;
    private final NewsMapper newsMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserLike add(UserLikeRequest request) {
        UserLike entity = UserLike.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        entity.setCreateById(userId);
        Long count = baseMapper.selectCount(new QueryWrapper<UserLike>().eq("user_id", userId).eq("news_id", request.getNewsId()));
        if (count == 0L) {
            baseMapper.insert(entity);
            News news = newsMapper.selectById(request.getNewsId());
            news.setLikes(news.getLikes() == null ? 0 : news.getLikes() + 1);
            newsMapper.updateById(news);
        }
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UserLikeRequest request) {
        UserLike entity = UserLike.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String newsId, String userId) {
        baseMapper.delete(new QueryWrapper<UserLike>().eq("news_id", newsId).eq("user_id", userId));
        News news = newsMapper.selectById(newsId);
        news.setLikes(news.getLikes() - 1);
        newsMapper.updateById(news);
    }

    @Override
    public UserLike getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<UserLike> pageList(UserLikeRequest request) {
        Page<UserLike> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<UserLike> query = this.buildWrapper(request);
        IPage<UserLike> pageRecord = baseMapper.selectPage(page, query);
        List<UserLike> records = pageRecord.getRecords();
        List<UserLike> recordsNew = new ArrayList<>();
        for (UserLike userLike : records) {
            News news = newsMapper.selectById(userLike.getNewsId());
            SysUser sysUser = userMapper.selectById(userLike.getUserId());
            userLike.setNewTitle(news == null ? "" : news.getTitle());
            userLike.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(userLike);
        }
        if (StringUtils.isNotBlank(request.getNewTitle())) {
            recordsNew = recordsNew.stream().filter(e -> e.getNewTitle().contains(request.getNewTitle())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<UserLike> list(UserLikeRequest request) {
        LambdaQueryWrapper<UserLike> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<UserLike> buildWrapper(UserLikeRequest param) {
        LambdaQueryWrapper<UserLike> query = new LambdaQueryWrapper<>();
        return query;
    }

}
